<%_
var i18nToLoad = [entityInstance];
for (var idx in fields) {
    if (fields[idx].fieldIsEnum == true) {
        i18nToLoad.push(fields[idx].enumInstance);
    }
}
_%>

    <%= entityInstancePlural %>: <%= entityClass %>[];
    currentAccount: any;
    eventSubscriber: Subscription;
    itemsPerPage: number;
    links: any;
    page: any;
    predicate: any;
    queryCount: any;
    reverse: any;
    totalItems: number;
    <%_ if (searchEngine === 'elasticsearch') { _%>
    currentSearch: string;
    <%_ } _%>

    constructor(
        <%_ if (enableTranslation) { _%>
        private jhiLanguageService: JhiLanguageService,
        <%_ } _%>
        private <%= entityInstance %>Service: <%= entityClass %>Service,
        private alertService: AlertService,
        <%_ if (fieldsContainBlob) { _%>
        private dataUtils: DataUtils,
        <%_ } _%>
        private eventManager: EventManager,
        private parseLinks: ParseLinks,
        <%_ if (searchEngine === 'elasticsearch') { _%>
        private activatedRoute: ActivatedRoute,
        <%_ } _%>
        private principal: Principal
    ) {
        this.<%= entityInstancePlural %> = [];
        this.itemsPerPage = ITEMS_PER_PAGE;
        this.page = 0;
        this.links = {
            last: 0
        };
        this.predicate = 'id';
        this.reverse = true;
        <%_ if (searchEngine === 'elasticsearch') { _%>
        this.currentSearch = activatedRoute.snapshot.params['search'] ? activatedRoute.snapshot.params['search'] : '';
        <%_ } _%>
        <%_ if (enableTranslation) { _%>
        this.jhiLanguageService.setLocations(<%- toArrayString(i18nToLoad) %>);
        <%_ } _%>
    }

    loadAll () {
    <%_ if (searchEngine === 'elasticsearch') { _%>
        if (this.currentSearch) {
            this.<%= entityInstance %>Service.search({
                query: this.currentSearch,
                page: this.page,
                size: this.itemsPerPage,
                sort: this.sort()
            }).subscribe(
                (res: Response) => this.onSuccess(res.json(), res.headers),
                (res: Response) => this.onError(res.json())
            );
            return;
        }
    <%_ } _%>
        this.<%= entityInstance %>Service.query({
            page: this.page,
            size: this.itemsPerPage,
            sort: this.sort()
        }).subscribe(
            (res: Response) => this.onSuccess(res.json(), res.headers),
            (res: Response) => this.onError(res.json())
        );
    }

    reset () {
        this.page = 0;
        this.<%= entityInstancePlural %> = [];
        this.loadAll();
    }

    loadPage(page) {
        this.page = page;
        this.loadAll();
    }
    <%_ if (searchEngine === 'elasticsearch') { _%>

    clear () {
        this.<%= entityInstancePlural %> = [];
        this.links = {
            last: 0
        };
        this.page = 0;
        this.predicate = 'id';
        this.reverse = true;
        this.currentSearch = '';
        this.loadAll();
    }

    search (query) {
        if (!query) {
            return this.clear();
        }
        this.<%= entityInstancePlural %> = [];
        this.links = {
            last: 0
        };
        this.page = 0;
        this.predicate = '_score';
        this.reverse = false;
        this.currentSearch = query;
        this.loadAll();
    }
    <%_ } _%>
